﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSys.Model;
using System.Data.SqlClient;
using System.Data;

namespace HRMSys.DAL
{
    public class OperatorDAL
    {
        /// <summary>
        /// 新增一条数据(Operator)
        /// </summary>
        /// <param name="op"></param>
        public int Insert(Operator op)
        {
            string sql = @"insert into T_Operator(Id,UserName,Password,IsDeleted,RealName,IsLocked) 
                           values(newid(),@UserName,@Password,0,@RealName,0)";
            SqlParameter userNameParam = new SqlParameter("@UserName", op.UserName);
            SqlParameter passwordParam = new SqlParameter("@password", op.Password);
            //SqlParameter isDeletedParam = new SqlParameter("@IsDeleted",0);
            SqlParameter realNameParam = new SqlParameter("@RealName", op.RealName);
            //SqlParameter isLockedParam = new SqlParameter("@IsLocked",op.IsLocked);

            int result = SqlHelper.ExecuteNonQuery(sql, userNameParam, passwordParam, realNameParam);
            return result;
        }

        /// <summary>
        /// 根据ID删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteById(Guid id)
        {
            string sql = "Update T_Operator set IsDeleted=1 where Id=@id";
            SqlParameter idParam = new SqlParameter("@id", id);
            int result = SqlHelper.ExecuteNonQuery(sql, idParam);
            return result;
        }

        /// <summary>
        /// 获得所有操作员
        /// </summary>
        /// <returns></returns>
        public Operator[] GetAll()
        {
            string sql = "select * from T_Operator where IsDeleted=0 and IsDeleted=0";
            DataTable table = SqlHelper.ExecuteDataTable(sql);
            Operator[] operators = new Operator[table.Rows.Count];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                operators[i] = ToOperator(table.Rows[i]);
            }
            return operators;
        }

        /// <summary>
        /// 组装Operator对象
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private Operator ToOperator(DataRow row)
        {
            Operator op = new Operator();
            op.Id = (Guid)row["Id"];
            op.UserName = (string)row["UserName"];
            op.Password = (string)row["Password"];
            op.IsDeleted = (bool)row["IsDeleted"];
            op.RealName = (string)row["RealName"];
            op.IsLocked = (bool)row["IsLocked"];
            return op;
        }

        /// <summary>
        /// 根据用户名查询用户
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public Operator GetUserByName(string userName)
        {
            string sql = "select * from T_Operator where UserName=@UserName";
            SqlParameter userNameParam = new SqlParameter("@UserName", userName);

            DataTable table = SqlHelper.ExecuteDataTable(sql, userNameParam);
            int dataCount = table.Rows.Count;
            if (dataCount <= 0)
            {
                return null;
            }
            else if (table.Rows.Count > 1)
            {
                throw new Exception("用户名有重复!");
            }
            else
            {
                Operator op = ToOperator(table.Rows[0]);
                return op;
            }
        }

        /// <summary>
        /// 根据Id获取操作员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Operator GetById(Guid id)
        {
            string sql = "select * from T_Operator where Id=@id";
            SqlParameter idParam = new SqlParameter("@id", id);

            DataTable table = SqlHelper.ExecuteDataTable(sql, idParam);
            int dataCount = table.Rows.Count;
            if (dataCount <= 0)
            {
                return null;
            }
            else if (dataCount > 1)
            {
                throw new Exception("用户ID有重复!");
            }
            else
            {
                Operator op = ToOperator(table.Rows[0]);
                return op;
            }
        }

        /// <summary>
        /// 根据Id更新操作员(用户名,姓名)
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="realName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Update(string userName, string realName, Guid id) 
        {
            int result;

            string sql = "Update T_Operator set UserName=@UserName,RealName=@RealName where Id=@Id";
            SqlParameter userNameParam = new SqlParameter("@UserName",userName);
            SqlParameter realNameParam = new SqlParameter("@RealName", realName);
            SqlParameter idParam = new SqlParameter("@Id", id);

            result = SqlHelper.ExecuteNonQuery(sql, userNameParam, realNameParam, idParam);
            return result;
        }

        /// <summary>
        /// 根据Id更新操作员(用户名,姓名,密码)
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="realName"></param>
        /// <param name="password"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Update(string userName, string realName, string password, Guid id) 
        {
            int result = 0;

            string sql = "Update T_Operator set UserName=@UserName,RealName=@RealName,Password=@Password where Id=@Id";
            SqlParameter userNameParam = new SqlParameter("@UserName", userName);
            SqlParameter realNameParam = new SqlParameter("@RealName", realName);
            SqlParameter pwdParam = new SqlParameter("@Password", password);
            SqlParameter idParam = new SqlParameter("@Id", id);

            result = SqlHelper.ExecuteNonQuery(sql, userNameParam, realNameParam, pwdParam, idParam);
            return result;
        }
    }
}
